package com.airS.task.fileMgr.utils.url;

import org.springframework.util.StringUtils;
import org.springframework.web.util.UrlPathHelper;

import javax.servlet.http.HttpServletRequest;

/**
 * url处理工具类
 * Created by alan on 2017/3/9.
 */
public abstract class UrlUtils {

    public static String getLookupPathForRequest(HttpServletRequest request) {
        UrlPathHelper pathHelper = new UrlPathHelper();
        String pathForRequest = pathHelper.getLookupPathForRequest(request);
        String queryString = pathHelper.getOriginatingQueryString(request);
        if (StringUtils.hasText(queryString))
            return pathForRequest + "?" + queryString;
        else
            return pathForRequest;
    }

    /**
     * 获得用户远程地址
     */
    public static String getRemoteAddr(HttpServletRequest request) {
        String remoteAddr = request.getHeader("X-Real-IP");
        if (!StringUtils.isEmpty(remoteAddr)) {
            remoteAddr = request.getHeader("X-Forwarded-For");
        } else if (!StringUtils.isEmpty(remoteAddr)) {
            remoteAddr = request.getHeader("Proxy-Client-IP");
        } else if (!StringUtils.isEmpty(remoteAddr)) {
            remoteAddr = request.getHeader("WL-Proxy-Client-IP");
        }
        return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
    }

}
